<template>
  <el-dialog width="600px" :title="dialogTitle" :visible.sync="dialogVisible" @close="handleClose">
    <el-form
      ref="submitForm"
      v-loading="formLoading"
      :model="submitForm"
      :rules="submitFormRules"
      label-position="right"
      label-width="80px"
      class="submit-form"
    >
      <el-form-item v-if="submitForm.parentId" label="上级部门" prop="parentId">
        <el-input v-model="submitForm.parentName" @click="deptTreeClick">
          <el-button slot="append" type="primary" class="el-icon-search" @click="deptTreeClick" />
        </el-input>
      </el-form-item>
      <el-form-item label="部门名称" prop="deptName">
        <el-input v-model="submitForm.deptName" placeholder="请输入部门名称" />
      </el-form-item>
      <el-form-item label="排序" prop="sort">
        <el-input-number v-model="submitForm.sort" style="width: 100%" placeholder="请输入排序" />
      </el-form-item>
      <el-form-item label="状态" prop="state">
        <el-switch
          v-model="submitForm.state"
          active-color="#13ce66"
          inactive-color="#ff4949"
          active-text="可用"
          inactive-text="禁用"
          active-value="1"
          inactive-value="0"
        />
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button :loading="formLoading" type="text" @click="handleClose">取消</el-button>
      <el-button :loading="formLoading" type="primary" @click="handleSubmit">提交</el-button>
    </span>
    <el-dialog title="选择部门" width="600px" append-to-body :visible.sync="deptTreeVisible">
      <el-tree :data="deptTreeData" :props="{children: 'children',label: 'deptName'}" @node-click="submitDeptTreeClick" />
    </el-dialog>
  </el-dialog>
</template>

<script>
import { updateDept, saveDept, selectDeptById, selectDeptTree } from '@/api/system/dept'
/**
 * @author yizuomin
 * @date 2023/5/7 19:05
 **/
export default {
  name: 'SubmitModule',
  props: {
    visible: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      dialogVisible: this.visible,
      deptTreeVisible: false,
      dialogTitle: '',
      formLoading: false,
      deptTreeData: [],
      submitForm: {
        id: '',
        deptName: '',
        parentId: '',
        parentName: '',
        state: '1',
        sort: 0
      },
      submitFormRules: {
        deptName: [{ required: true, message: '请输入部门名称', trigger: 'change' }],
        parentId: [{ required: true, message: '请输入上级部门', trigger: 'change' }],
        sort: [{ required: true, message: '请输入排序', trigger: 'change' }]
      }
    }
  },
  methods: {
    init(parent, id) {
      if (parent) {
        this.submitForm.parentId = parent.id
        this.submitForm.parentName = parent.deptName
      }
      if (!id) {
        this.dialogTitle = '新增部门'
        return
      }
      this.dialogTitle = '修改部门'
      this.formLoading = true
      selectDeptById(id).then(resp => {
        if (resp.success) {
          this.submitForm = resp.data
        }
      }).finally(() => {
        this.formLoading = false
      })
    },
    // 提交
    handleSubmit() {
      this.$refs.submitForm.validate((valid) => {
        if (valid) {
          this.formLoading = true
          let request = saveDept
          if (this.submitForm.id) {
            request = updateDept
          }
          request(this.submitForm).then(resp => {
            if (resp.success) {
              this.$message.success('操作成功')
              this.$refs.submitForm.resetFields()
              this.handleClose()
            }
          }).finally(() => {
            this.formLoading = false
          })
        }
      })
    },
    // 机构树
    deptTreeClick() {
      this.deptTreeVisible = false
      selectDeptTree({}).then(resp => {
        if (resp.success) {
          this.deptTreeData = resp.data
        }
      }).finally(() => {
        this.deptTreeVisible = true
      })
    },
    // 机构树点击
    submitDeptTreeClick(row) {
      if (row.id === this.submitForm.id) {
        this.$message.warning('上级机构不能是自己')
        return
      }
      this.submitForm.parentName = row.deptName
      this.submitForm.parentId = row.id
      this.deptTreeVisible = false
    },
    handleClose() {
      this.$emit('close')
    }
  }
}
</script>

<style scoped>

</style>
